package com.lanswon.carlocationsearch.util;

import com.lanswon.carlocationsearch.dto.ImportDataDto;
import lombok.experimental.UtilityClass;

import java.util.List;

/**
 * @Description
 * @Author zsw
 * @Date 2019/12/4 13:54
 * @Version V1.0
 **/
@UtilityClass
public class CommonUtil {

    private String CAR_NUM_TEMPLATE =" CAR_NUM = '%s'";

    private String DATE_TEMPLATE =" AND TO_CHAR (CAP_DATE, 'YYYY-MM-DD HH24:MI:SS') <= '%s'";
    /**
     * List转String:;
     *
     *(
     *                     CAR_NUM = '苏B0T512'
     *                     AND TO_CHAR (
     *                     CAP_DATE,
     *                     'yyyy/MM/dd HH24:mi'
     *                     ) <= '2016/12/01 00:23'
     *                 )
     *                 OR (
     *                     CAR_NUM = '苏B5T527'
     *                     AND TO_CHAR (
     *                     CAP_DATE,
     *                     'YYYY-MM-DD HH24-MI-SS'
     *                     ) <= '2019/12/05 14:01:46'
     *                 )...
     */
    public String ListConventString(List<ImportDataDto> list, int count){
        StringBuffer sb  = new StringBuffer();
        sb.append(" SELECT  CAR_NUM carNum, TO_CHAR ( CAP_DATE, 'yyyy/MM/dd HH24:mi:ss' ) capDate, DEV_NAME devName  FROM ( SELECT CAR_NUM, CAP_DATE, DEV_NAME, ROW_NUMBER() OVER( PARTITION BY CAR_NUM ORDER BY CAP_DATE DESC ) AS RN FROM C_PICRECORD ");





        ImportDataDto cd = list.get(0);
        sb.append(" where ");
        sb.append(" ( ");
        sb.append(String.format(CAR_NUM_TEMPLATE,cd.getCarNum()));
        sb.append(String.format(DATE_TEMPLATE,cd.getDateTime()));
        sb.append(" ) ");
        for(int i=1;i<list.size();i++){
            sb.append(" or ");
            sb.append(" ( ");
            sb.append(String.format(CAR_NUM_TEMPLATE,list.get(i).getCarNum()));
            sb.append(String.format(DATE_TEMPLATE,list.get(i).getDateTime()));
            sb.append(" ) ");

        }

        sb.append(" ) ");

        sb.append( "WHERE RN  <= "+count);

        return sb.toString();
    }
}